package com.example.lotterysystem.common.pojo;

import com.example.lotterysystem.common.errorcode.ErrorCode;
import com.example.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * @auther falls_vc
 * description:
 * @date 2025/5/7  20:55
 */
@Data
public class CommonResult<T> implements Serializable {
    /**
     * 操作状态码
     */
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 返回信息
     */
    private String msg;

    public static <T>  CommonResult<T> success(T data){
        CommonResult<T> commonResult=new CommonResult<>();
        commonResult.code= GlobalErrorCodeConstants.SUCEESS.getCode();
        commonResult.data=data;
        commonResult.msg="";
        return commonResult;
    }
    public static <T>  CommonResult<T> error(Integer code,String msg){
        Assert.isTrue(!(GlobalErrorCodeConstants.SUCEESS.getCode()==code),"code不是错误异常");
        CommonResult<T> commonResult=new CommonResult<>();
        commonResult.code= code;
        commonResult.data=null ;
        commonResult.msg=msg;
        return commonResult;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode){
        return error(errorCode.getCode(),errorCode.getMsg());
    }

}
